﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers{
public class SalaryDataController : SalaryDataControllerBase{
	public SalaryDataController()
		: base()
	{
	}	
	private DataTable _Table;
	private SalaryDataEntity oSalaryData;	
	public SalaryDataEntity SalaryDataObject{
		get{
			return oSalaryData;
		}
		set{
			oSalaryData=value;
		}
	}
	public DataTable Table{
		get{
			if(_Table==null&&AllowCaching){				
				_Table=Cache.Get<DataTable>(CACHE_KEY_DS);
			}
			return _Table;
		}
		set{
			_Table=value;
		}
	}
	public void LoadAllDSSalaryData(bool clearCache){
		bool bForce=clearCache;
		if(bForce||_Table==null)
		{
			DataSet ds=Service.SelectAllDS();
			_Table=ds.Tables[0];
			AddIndexToTable(_Table);
			if(AllowCaching)
				AddCache(CACHE_KEY_DS,_Table);			
		}				
	}	
	public void AddIndexToTable(){
		AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		ResetTableIndex(_Table);
	}
	public void LoadObject(Int32 SalaryDataId){
		SalaryDataEntity oLoad=FindSalaryData(SalaryDataId);
		if(oLoad==null){
			LoadAllSalaryData(true);
			oSalaryData=FindSalaryData(SalaryDataId);
		}
		else{
			oSalaryData=oLoad;
		}
	}
	public bool AddNew(){
		if(oSalaryData==null)return false;
		bool result = Service.Save(oSalaryData);
		if(result){
			AddObjectToCache(oSalaryData);
		}
		return result;
	}
	public bool Update(){
		if(oSalaryData==null)return false;
		return Service.Update(oSalaryData);	
	}
	public bool Delete(){
		if(oSalaryData==null)return false;		
		bool retvalue = Service.Delete(oSalaryData);
		if(retvalue){			
			RemoveObjectFromCache(oSalaryData);
		}
		return retvalue;
	}	
	public bool DeleteByID(Int32 SalaryDataId){
		bool retvalue = Service.DeleteByID(SalaryDataId);		
		if(retvalue){
			SalaryDataEntity oDeleted=FindSalaryData(SalaryDataId);
			RemoveObjectFromCache(oDeleted);
		}
		return retvalue;
	}	
	public void ResetObject(){
		this.oSalaryData = new SalaryDataEntity();
	}
	public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.SalaryDataObject,row);
    }
    public SalaryDataEntity LoadSalaryData(Int32 SalaryDataId){			
		SalaryDataEntity o=Service.SelectOne(SalaryDataId);		
		return o;
	}
	#region Cached
    private static List<SalaryDataEntity>_ListSalaryData;
	private static bool _AllowCaching=true;
	public static bool AllowCaching{
		get{return _AllowCaching;}
		set{AllowCaching=value;}
	}	
	public static void ClearCache(){
			if(!_AllowCaching)return;
			Cache.Remove(CACHE_KEY_DS);
			Cache.Remove(CACHE_KEY_LS);
	}
	private const string CACHE_KEY_DS="SalaryDataDS";
	private const string CACHE_KEY_LS="SalaryDataLS";
	private void AddObjectToCache(SalaryDataEntity newObject){
		if(!AllowCaching)return;
		GetCacheList().Add(newObject);
	}
	private void RemoveObjectFromCache(SalaryDataEntity oRemove)
	{
		if(!AllowCaching)return;
		if(oRemove!=null)
		GetCacheList().Remove(oRemove);
	}
	private static List<SalaryDataEntity>GetCacheList(){
		if(Cache==null)return default(List<SalaryDataEntity>);
		List<SalaryDataEntity>ls= Cache.Get<List<SalaryDataEntity>>(CACHE_KEY_LS);
		if(ls==null){
			ls = new List<SalaryDataEntity>();
			AddCache(CACHE_KEY_LS,ls);
		}
		return ls;
	}
	public List<SalaryDataEntity>ListSalaryData{
		get{
			return LoadAllSalaryData(false);
		}
	}
	public static List<SalaryDataEntity>ListSalaryDataStatic{
		get{
			LoadAllSalaryData(false);
			return _ListSalaryData;
		}
	}
	public SalaryDataEntity FindSalaryData(Int32 SalaryDataId){
		if(AllowCaching){
			_ListSalaryData=GetCacheList();
		}		
		SalaryDataEntity oResult=_ListSalaryData.Find(delegate(SalaryDataEntity oFind){
			bool result=true; 
			result&=oFind.SalaryDataId.Equals(SalaryDataId);
			return result;
		 });
		return oResult;
	}
	public static List<SalaryDataEntity>LoadAllSalaryData(bool clearCache){
		_ListSalaryData = new List<SalaryDataEntity>();
		if(AllowCaching){
			_ListSalaryData=GetCacheList();
		}
		if(_ListSalaryData==null||_ListSalaryData.Count==0)
			clearCache=true;
		if(!clearCache)return _ListSalaryData;
		_ListSalaryData=new List<SalaryDataEntity>(Service.SelectAll());		
		if(AllowCaching)
			AddCache(CACHE_KEY_LS,_ListSalaryData);
		return _ListSalaryData;
	}
	#endregion
}
}
